package com.hudongwx.shansun.fragment;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.hudongwx.shansun.activity.DisplayActivity;
import com.hudongwx.shansun.R;
import com.hudongwx.shansun.utlis.API;


public class Fragment3 extends Fragment {

    private ProgressBar progressBar;
    private WebView webView;
    private View view;
    private String TAG = "Fragment3";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (null == view) {
            view = inflater.inflate(R.layout.fragment_fragment3, container, false);
            findId();
            progressBar.setMax(100);
         webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl("http://hd.shandjj.com/activity/newpublic");

            webView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getActivity(),DisplayActivity.class));
                }
            });
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    Uri uri = Uri.parse(url);
                    if ("sdjj".equals(uri.getScheme())) {
                        //判断如果是闪电降价的协议，就启动
                        Log.i(TAG, "shouldOverrideUrlLoading: ");
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_VIEW);
                        intent.setData(uri);
                        startActivity(intent);
                        return false;
                    }
                    return true;
                }
            });
            webView.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    if (100 == newProgress) {
                        progressBar.setVisibility(View.INVISIBLE);
                    } else {
                        progressBar.setProgress(newProgress);
                        progressBar.setVisibility(View.VISIBLE);
                    }
                }
            });
        }

        return view;
    }

    private void findId() {
        progressBar = (ProgressBar) view.findViewById(R.id.fragment_3_pro);
        webView = (WebView) view.findViewById(R.id.fragment_3_web);

    }


}
